import java.util.Scanner;

public class Test {

    public static void modify(Animal[] animal) throws Exception {
        Scanner scanner = new Scanner(System.in);
        System.out.println("动物名字\t腿的条数\t动物叫声");
        for (int i = 0; i < animal.length; i++) {
            if (animal[i] instanceof Cat) {
                Cat cat = (Cat) animal[i];
                System.out.print(cat.getName() + "\t" + cat.getLegNum() + "\t");
                cat.shout();
            } else if (animal[i] instanceof Duck) {
                Duck duck = (Duck) animal[i];
                System.out.print(duck.getName() + "\t" + duck.getLegNum() + "\t");
                duck.shout();
            } else if (animal[i] instanceof Dolphin) {
                Dolphin dolphin = (Dolphin) animal[i];
                System.out.print(dolphin.getName() + "\t" + dolphin.getLegNum() + "\t");
                dolphin.shout();
            }
        }
        while (true) {
            System.out.println("是否要继续修改数据：按0进行修改操作，其他任意数字键退出");
            int num = scanner.nextInt();
            if (num!=0){
                System.out.println("退出了");
                break;
            }
            System.out.println("请输入要修改的动物(1,猫 2，鸭子 3, 海豚)");
            int num1=scanner.nextInt();
            if (num1==1){
            System.out.println("请输入猫的名称:");
            String name=scanner.next();
            System.out.println("请输入猫腿的条数");
            int LegNum=scanner.nextInt();
                if (LegNum!=4){
                   throw new Exception("错误！猫只能有四条腿喔");
                }
                System.out.println("动物名字\t腿的条数\t动物叫声");
                for (int i = 0; i < animal.length; i++) {
                    if (animal[i] instanceof Cat) {
                        Cat cat = (Cat) animal[i];
                        cat.setName(name);
                        cat.setLegNum(LegNum);
                        System.out.print(cat.getName() + "\t" + cat.getLegNum() + "\t");
                        cat.shout();
                    } else if (animal[i] instanceof Duck) {
                        Duck duck = (Duck) animal[i];
                        System.out.print(duck.getName() + "\t" + duck.getLegNum() + "\t");
                        duck.shout();
                    } else if (animal[i] instanceof Dolphin) {
                        Dolphin dolphin = (Dolphin) animal[i];
                        System.out.print(dolphin.getName() + "\t" + dolphin.getLegNum() + "\t");
                        dolphin.shout();
                    }
                }
            }else if (num1==2){
                System.out.println("请输入鸭子的名称:");
                String name=scanner.next();
                System.out.println("请输入鸭腿的条数");
                int LegNum=scanner.nextInt();
                if (LegNum!=2){
                    throw new Exception("错误！鸭子有只能有两条腿哦。。");
                }
                System.out.println("动物名字\t腿的条数\t动物叫声");
                for (int i = 0; i < animal.length; i++) {
                    if (animal[i] instanceof Cat) {
                        Cat cat = (Cat) animal[i];
                        System.out.print(cat.getName() + "\t" + cat.getLegNum() + "\t");
                        cat.shout();
                    } else if (animal[i] instanceof Duck) {
                        Duck duck = (Duck) animal[i];
                        duck.setName(name);
                        duck.setLegNum(LegNum);
                        System.out.print(duck.getName() + "\t" + duck.getLegNum() + "\t");
                        duck.shout();
                    } else if (animal[i] instanceof Dolphin) {
                        Dolphin dolphin = (Dolphin) animal[i];
                        System.out.print(dolphin.getName() + "\t" + dolphin.getLegNum() + "\t");
                        dolphin.shout();
                    }
                }
            }else if (num1==3){
                System.out.println("请输入海豚的名称:");
                String name=scanner.next();
                System.out.println("请输入海豚腿的条数");
                int LegNum=scanner.nextInt();
                if (LegNum!=0){
                    throw new Exception("错误！海豚没有腿哦。。");
                }
                System.out.println("动物名字\t腿的条数\t动物叫声");
                for (int i = 0; i < animal.length; i++) {
                    if (animal[i] instanceof Cat) {
                        Cat cat = (Cat) animal[i];
                        System.out.print(cat.getName() + "\t" + cat.getLegNum() + "\t");
                        cat.shout();
                    } else if (animal[i] instanceof Duck) {
                        Duck duck = (Duck) animal[i];
                        System.out.print(duck.getName() + "\t" + duck.getLegNum() + "\t");
                        duck.shout();
                    } else if (animal[i] instanceof Dolphin) {
                        Dolphin dolphin = (Dolphin) animal[i];
                        dolphin.setName(name);
                        dolphin.setLegNum(LegNum);
                        System.out.print(dolphin.getName() + "\t" + dolphin.getLegNum() + "\t");
                        dolphin.shout();
                    }
                }
            }

            }
        }

    public void load(Animal[] animal) {
        System.out.println("动物名字\t腿的条数\t动物叫声");
        for (int i = 0; i < animal.length; i++) {
                if (animal[i] instanceof Cat){
                    Cat cat=(Cat) animal[i];
                    System.out.print(cat.getName()+"\t"+cat.getLegNum()+"\t");
                    cat.shout();
                }else if (animal[i] instanceof Duck){
                    Duck duck = (Duck) animal[i];
                    System.out.print(duck.getName()+"\t"+duck.getLegNum()+"\t");
                    duck.shout();
                }else if (animal[i] instanceof Dolphin){
                    Dolphin dolphin = (Dolphin) animal[i];
                    System.out.print(dolphin.getName()+"\t"+dolphin.getLegNum()+"\t");
                    dolphin.shout();
                }
        }
    }
    public static void main(String[] args)  {
        Test test = new Test();
        Animal[] animal={new Cat("加菲猫",4),new Duck("唐小鸭",2),new Dolphin("海豚琪琪",0)};
      /* try {
           test.load(animal);
       } catch (CatException c){
           System.out.println(c.getMessage());
       }catch (DuckException d){
           System.out.println(d.getMessage());
       }catch (DolphinException d){
           System.out.println(d.getMessage());
       }catch (Exception e){
           e.printStackTrace();
       }*/

       try {
            Test.modify(animal);
        } catch (Exception e) {
            //System.out.println(e.getMessage());
            e.printStackTrace();
        }

    }
}
